package com.xingongchang.baolebei.protocol;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.external.activeandroid.annotation.Column;
import com.external.activeandroid.annotation.Table;

@Table(name = "User_tan")
public class User_tan {
	@Column(name = "cat_id")
	public  int cat_id;
	@Column(name = "cat_name")
	public String cat_name;
	@Column(name = "cat_desc")
	public String cat_desc;
	@Column(name = "thumb")
	public String thumb;
//	@Column(name = "sort_order")
//	public String sort_order;
	
	public ArrayList<User_tan> children = new ArrayList<User_tan>();

	public static User_tan fromJson(JSONObject jsonObject) throws JSONException {
		if (null == jsonObject) {
			return null;
		}

		User_tan localItem = new User_tan();

		JSONArray subItemArray;

		localItem.cat_id = jsonObject.optInt("cat_id");

		localItem.cat_name = jsonObject.optString("cat_name");
		
		localItem.cat_desc = jsonObject.optString("cat_desc");
		
		localItem.thumb = jsonObject.optString("thumb");
		
//		localItem.sort_order = jsonObject.optString("sort_order");
		subItemArray = jsonObject.optJSONArray("children");
		if (null != subItemArray) {
			for (int i = 0; i < subItemArray.length(); i++) {
				JSONObject subItemObject = subItemArray.getJSONObject(i);
				User_tan subItem = User_tan.fromJson(subItemObject);
				localItem.children.add(subItem);
			}
		}

		return localItem;
	}

	public JSONObject toJson() throws JSONException {
		JSONObject localItemObject = new JSONObject();
		JSONArray itemJSONArray = new JSONArray();
		localItemObject.put("cat_id", cat_id);
		localItemObject.put("cat_name", cat_name);
		localItemObject.put("cat_desc", cat_desc);
		localItemObject.put("thumb", thumb);
		
		for (int i = 0; i < children.size(); i++) {
			User_tan itemData = children.get(i);
			JSONObject itemJSONObject = itemData.toJson();
			itemJSONArray.put(itemJSONObject);
		}
		localItemObject.put("children", itemJSONArray);
		return localItemObject;
	}

	
}
